//
//  DKPanCardAuthKtpCell.m
//  DanKU
//
//  Created by 吴斌 on 2023/11/5.
//

#import "DKPanCardAuthKtpCell.h"
#import "DKPanCardMenberView.h"
static NSString * DKPanCardAuthKtpCellId = @"DKPanCardAuthKtpCellId";
@interface DKPanCardAuthKtpCell()
@property (nonatomic, strong)UIView * dk_PanCardBgView;
@property (nonatomic, strong)UIImageView * dk_PanCardPhotoView;
@property (nonatomic, strong)DKPanCardMenberView * dk_PanCardMenberView;
@property (nonatomic, strong)UIButton * dk_PanCardBtn;
@end
@implementation DKPanCardAuthKtpCell
+ (instancetype)authPanCardAuthKtpCell:(UITableView *)tableView{
    DKPanCardAuthKtpCell * dk_PanCardAuthKtpCell = [tableView dequeueReusableCellWithIdentifier:DKPanCardAuthKtpCellId];
    if (!dk_PanCardAuthKtpCell) {
        dk_PanCardAuthKtpCell = [[DKPanCardAuthKtpCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:DKPanCardAuthKtpCellId];
    }
    dk_PanCardAuthKtpCell.backgroundColor = [UIColor clearColor];
    dk_PanCardAuthKtpCell.selectionStyle = UITableViewCellSelectionStyleNone;
    dk_PanCardAuthKtpCell.accessoryType = UITableViewCellAccessoryNone;
    return dk_PanCardAuthKtpCell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self dk_addSubView];
    }
    return self;
}
- (void)dk_addSubView{
    WeakSelf(self)
    self.dk_PanCardBgView = [[UIView alloc] init];
    self.dk_PanCardBgView.backgroundColor = [UIColor whiteColor];
    self.dk_PanCardBgView.layer.cornerRadius = DK_IsWidthScale(50);
    [self.contentView addSubview:self.dk_PanCardBgView];
    [self.dk_PanCardBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(0)));
        make.right.equalTo(@(-DK_IsWidthScale(0)));
        make.top.equalTo(@(DK_IsWidthScale(20)));
        make.bottom.equalTo(@(-DK_IsWidthScale(0)));
    }];
    UIView * dk_PanCardBottomView = [[UIView alloc] init];
    dk_PanCardBottomView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:dk_PanCardBottomView];
    [dk_PanCardBottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(0)));
        make.right.equalTo(@(-DK_IsWidthScale(0)));
        make.height.equalTo(@(DK_IsWidthScale(50)));
        make.bottom.equalTo(@(-DK_IsWidthScale(0)));
    }];
    
    UILabel * dk_AuthCardHeadLab = [[UILabel alloc] init];
    dk_AuthCardHeadLab.textColor = DK_Hex(@"#333333");
    dk_AuthCardHeadLab.text = @"Click the photo take it again";
    dk_AuthCardHeadLab.font = DK_FONT_Medium(14);
    dk_AuthCardHeadLab.textAlignment = NSTextAlignmentCenter;
    [self.dk_PanCardBgView addSubview:dk_AuthCardHeadLab];
    [dk_AuthCardHeadLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(0)));
        make.right.equalTo(@(DK_IsWidthScale(0)));
        make.top.equalTo(@(DK_IsWidthScale(38)));
        make.height.equalTo(@(DK_IsWidthScale(20)));
    }];
    UIImageView * dk_AuthCardCenterImgView = [[UIImageView alloc] init];
    dk_AuthCardCenterImgView.userInteractionEnabled = YES;
    dk_AuthCardCenterImgView.image = DK_GetImage(@"dk_AuthCardKuang");
    [self.dk_PanCardBgView  addSubview:dk_AuthCardCenterImgView];
    [dk_AuthCardCenterImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.dk_PanCardBgView);
        make.top.equalTo(dk_AuthCardHeadLab.mas_bottom).offset(DK_IsWidthScale(15));
        make.width.equalTo(@(DK_IsWidthScale(241)));
        make.height.equalTo(@(DK_IsWidthScale(155)));
    }];
    self.dk_PanCardPhotoView = [[UIImageView alloc] init];
    self.dk_PanCardPhotoView.userInteractionEnabled = YES;
    self.dk_PanCardPhotoView.image = DK_GetImage(@"dk_AuthCardDefalt");
    [self.dk_PanCardBgView  addSubview:self.dk_PanCardPhotoView];
    [self.dk_PanCardPhotoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(dk_AuthCardCenterImgView).offset(DK_IsWidthScale(15));
        make.top.equalTo(dk_AuthCardCenterImgView).offset(DK_IsWidthScale(20));
        make.right.equalTo(dk_AuthCardCenterImgView).offset(-DK_IsWidthScale(15));
        make.bottom.equalTo(dk_AuthCardCenterImgView).offset(-DK_IsWidthScale(20));
    }];
    UIButton * dk_PanCardPhotoAgainBtn = [[UIButton alloc] init];
    [dk_PanCardPhotoAgainBtn addTarget:self action:@selector(startPanCard) forControlEvents:UIControlEventTouchUpInside];
    [self.dk_PanCardPhotoView addSubview:dk_PanCardPhotoAgainBtn];
    [dk_PanCardPhotoAgainBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@(DK_IsWidthScale(0)));
    }];
    
    UILabel * dk_AuthCardAlertLab = [[UILabel alloc] init];
    dk_AuthCardAlertLab.textColor = DK_Hex(@"#999999");
    dk_AuthCardAlertLab.text = @"Please confirm the following information";
    dk_AuthCardAlertLab.font = DK_FONT_Regular(14);
    [self.dk_PanCardBgView addSubview:dk_AuthCardAlertLab];
    [dk_AuthCardAlertLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(30)));
        make.right.equalTo(@(DK_IsWidthScale(0)));
        make.top.equalTo(dk_AuthCardCenterImgView.mas_bottom).offset(DK_IsWidthScale(33));
        make.height.equalTo(@(DK_IsWidthScale(20)));
    }];
    self.dk_PanCardMenberView = [[DKPanCardMenberView alloc] init];
    [self.dk_PanCardBgView addSubview:self.dk_PanCardMenberView];
    [self.dk_PanCardMenberView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(30)));
        make.right.equalTo(@(-DK_IsWidthScale(30)));
        make.top.equalTo(dk_AuthCardAlertLab.mas_bottom).offset(DK_IsWidthScale(20));
        make.height.equalTo(@(DK_IsWidthScale(100)));
    }];
    self.dk_PanCardMenberView.goCheckPanCardMenberBlock = ^ {
        [weakSelf updataNextStatus];
    };    
    self.dk_PanCardBtn = [[UIButton alloc] init];
    [self.dk_PanCardBtn setTitle:@"Next" forState:UIControlStateNormal];
    [self.dk_PanCardBtn setTitleColor:DK_Hex(@"#4566FF") forState:UIControlStateNormal];
    [self.dk_PanCardBtn setTitleColor:DK_Hex(@"#FFFFFF") forState:UIControlStateSelected];
    self.dk_PanCardBtn.titleLabel.font = DK_FONT_Semibold(16);
    [self.dk_PanCardBtn setBackgroundImage:DK_GetImage(@"dk_registerNextBg") forState:UIControlStateNormal];
    [self.dk_PanCardBtn setBackgroundImage:DK_GetImage(@"dk_registerNextSelectBg") forState:UIControlStateSelected];
    self.dk_PanCardBtn.userInteractionEnabled = false;
    [self.dk_PanCardBtn addTarget:self action:@selector(nextPanCard) forControlEvents:UIControlEventTouchUpInside];
    [self.dk_PanCardBgView addSubview:self.dk_PanCardBtn];
    [self.dk_PanCardBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DK_IsWidthScale(30)));
        make.right.equalTo(@(-DK_IsWidthScale(30)));
        make.height.equalTo(@(DK_IsWidthScale(50)));
        make.top.equalTo(self.dk_PanCardMenberView.mas_bottom).offset(DK_IsWidthScale(40));
    }];
    UIButton * dk_AuthCardPrivacyAgreementBtn = [[UIButton alloc] init];
    [dk_AuthCardPrivacyAgreementBtn setTitle:@"《Privacy Agreement》" forState:UIControlStateNormal];
    [dk_AuthCardPrivacyAgreementBtn setTitleColor:DK_Hex(@"#CCCCCC") forState:UIControlStateNormal];
    dk_AuthCardPrivacyAgreementBtn.titleLabel.font = DK_FONT_Regular(12);
    [dk_AuthCardPrivacyAgreementBtn addTarget:self action:@selector(checkCardPrivacyAgreement) forControlEvents:UIControlEventTouchUpInside];
    [self.dk_PanCardBgView addSubview:dk_AuthCardPrivacyAgreementBtn];
    [dk_AuthCardPrivacyAgreementBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.dk_PanCardBgView);
        make.top.equalTo(self.dk_PanCardBtn.mas_bottom).offset(DK_IsWidthScale(20));
        make.height.equalTo(@(DK_IsWidthScale(17)));
    }];
}
-(void)setDk_AuthPanCardMdl:(DKAuthPanCardModel *)dk_AuthPanCardMdl{
    _dk_AuthPanCardMdl = dk_AuthPanCardMdl;
    if(_dk_AuthPanCardMdl){
        self.dk_PanCardPhotoView.image = [NSString imageWithBase64String:_dk_AuthPanCardMdl.url];
        self.dk_PanCardMenberView.dk_AuthPanCardMdl = _dk_AuthPanCardMdl;
        [self updataNextStatus];
    }
}
- (void)updataNextStatus{
    NSString * panCardName = [self.dk_PanCardMenberView getPanCardName];
    NSString * panCardNo = [self.dk_PanCardMenberView getPanCardNo];
    if(panCardName.length&&panCardNo.length&&self.dk_PanCardPhotoView.image){
        self.dk_PanCardBtn.selected = YES;
        self.dk_PanCardBtn.userInteractionEnabled = YES;
    }else{
        self.dk_PanCardBtn.selected = NO;
        self.dk_PanCardBtn.userInteractionEnabled = NO;
    }
}
- (void)startPanCard{
    if(self.StartPanCardBlock){
        self.StartPanCardBlock();
    }
}
- (void)checkCardPrivacyAgreement{
    if(self.CheckPrivaryAgmentBlock){
        self.CheckPrivaryAgmentBlock();
    }
}
- (void)nextPanCard{
    if(self.NextPanCardBlock){
        self.dk_AuthPanCardMdl.idCardOcrDetails.name = [self.dk_PanCardMenberView getPanCardName];
        self.dk_AuthPanCardMdl.idCardOcrDetails.idNumber = [self.dk_PanCardMenberView getPanCardNo];
        self.NextPanCardBlock(self.dk_AuthPanCardMdl);
    }
}
//-(void)layoutSublayersOfLayer:(CALayer *)layer{
//    [super layoutSublayersOfLayer:layer];
//    [self.dk_PanCardBgView makeCornerOnTopleftRight:DK_IsWidthScale(50)];
//}
@end
